﻿using FR.SAFTAnalyser.Services;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FR.SAFTAnalyser.Model.Stocks
{
    public partial class StockHeader : BaseData, IDataErrorInfo
    {
        StockHeaderToolTipService tooltip;
        public StockHeaderToolTipService Tooltip
        {
            get
            {
                if (tooltip == null)
                    tooltip = new StockHeaderToolTipService();
                return tooltip;
            }
            set { tooltip = value; }
        }

        public string Error
        {
            get { return null; }
        }

        public string this[string columnName]
        {
            get
            {
                Error erro = null;
                if (columnName == "TaxRegistrationNumber")
                {
                    erro = ValidateTaxRegistrationNumber(appendError: true);
                }
                else if (columnName == "FileVersion")
                {
                    erro = ValidateFileVersion(appendError: true);
                }
                else if (columnName == "FiscalYear")
                {
                    erro = ValidateFiscalYear(appendError: true);
                }

                if (erro != null)
                    return erro.Description;
                else
                    return null;
            }
        }

        #region Validation
        public Error ValidateTaxRegistrationNumber(bool appendError = false)
        {
            Error erro = null;
            if (string.IsNullOrEmpty(this.TaxRegistrationNumber) == false && !SAFT.Validations.Validations.CheckTaxRegistrationNumber(this.TaxRegistrationNumber))
            {
                erro = new Error { Description = "NIF inválido", Field = "TaxRegistrationNumber", TypeofError = this.GetType(), Value = this.TaxRegistrationNumber, UID = this.Pk };
                if (appendError)
                    this.Tooltip.TaxRegistrationNumber = this.Tooltip.TaxRegistrationNumber.FormatTooltipWithError(erro.Description);
            }

            return erro;
        }
        public Error ValidateFileVersion(bool appendError = false)
        {
            Error erro = null;
            if (string.IsNullOrEmpty(this.FileVersion) == false && this.FileVersion.Length > 10)
            {
                erro = new Error { Description = "Versão do ficheiro de Stocks incorrecto.", Field = "FileVersion", TypeofError = this.GetType(), Value = this.FileVersion, UID = this.Pk };
                if (appendError)
                    this.Tooltip.FileVersion = this.Tooltip.FileVersion.FormatTooltipWithError(erro.Description);
            }
            return erro;
        }
        public Error ValidateFiscalYear(bool appendError = false)
        {
            Error erro = null;
            if (string.IsNullOrEmpty(this.FiscalYear) == false)
            {
                int ano = 0;
                Int32.TryParse(this.FiscalYear, out ano);
                if (this.FiscalYear.Length != 4 && ano == 0)
                {
                    erro = new Error { Description = "Ano fiscal incorrecto.", Field = "FiscalYear", TypeofError = this.GetType(), Value = this.FiscalYear, UID = this.Pk };
                    if (appendError)
                        this.Tooltip.FiscalYear = this.Tooltip.FiscalYear.FormatTooltipWithError(erro.Description);
                }
            }
            return erro;
        }
        #endregion Validation
    }

    public partial class Stock : BaseData, IDataErrorInfo
    {
        StockToolTipService tooltip;
        public StockToolTipService Tooltip
        {
            get
            {
                if (tooltip == null)
                    tooltip = new StockToolTipService();
                return tooltip;
            }
            set { tooltip = value; }
        }

        public string Error
        {
            get { return null; }
        }

        public string this[string columnName]
        {
            get
            {
                Error erro = null;
                if (columnName == "ClosingStockQuantity")
                {
                    erro = ValidateClosingStockQuantity(appendError: true);
                }
                else if (columnName == "UnitOfMeasure")
                {
                    erro = ValidateUnitOfMeasure(appendError: true);
                }
                else if (columnName == "ProductNumberCode")
                {
                    erro = ValidateProductNumberCode(appendError: true);
                }
                else if (columnName == "ProductDescription")
                {
                    erro = ValidateProductDescription(appendError: true);
                }
                else if (columnName == "ProductCode")
                {
                    erro = ValidateProductCode(appendError: true);
                }

                if (erro != null)
                    return erro.Description;
                else
                    return null;
            }
        }

        #region Validation
        public Error ValidateClosingStockQuantity(bool appendError = false)
        {
            Error erro = null;
            if (this.ClosingStockQuantity < 0)
            {
                erro = new Error { Description = "Quantidade incorrecta", Field = "ClosingStockQuantity", TypeofError = this.GetType(), Value = this.ClosingStockQuantity.ToString(), UID = this.Pk };
                if (appendError)
                    this.Tooltip.ClosingStockQuantity = this.Tooltip.ClosingStockQuantity.FormatTooltipWithError(erro.Description);
            }

            return erro;
        }
        public Error ValidateUnitOfMeasure(bool appendError = false)
        {
            Error erro = null;
            if (string.IsNullOrEmpty(this.UnitOfMeasure) || this.UnitOfMeasure.Length > 20)
            {
                erro = new Error { Description = "Unidade com valor incorrecto.", Field = "UnitOfMeasure", TypeofError = this.GetType(), Value = this.UnitOfMeasure, UID = this.Pk };
                if (appendError)
                    this.Tooltip.UnitOfMeasure = this.Tooltip.UnitOfMeasure.FormatTooltipWithError(erro.Description);
            }
            return erro;
        }
        public Error ValidateProductNumberCode(bool appendError = false)
        {
            Error erro = null;
            if (string.IsNullOrEmpty(this.ProductNumberCode) || this.ProductNumberCode.Length > 60)
            {
                erro = new Error { Description = "Código EAN incorrecto.", Field = "ProductNumberCode", TypeofError = this.GetType(), Value = this.ProductNumberCode, UID = this.Pk };
                if (appendError)
                    this.Tooltip.ProductNumberCode = this.Tooltip.ProductNumberCode.FormatTooltipWithError(erro.Description);
            }
            return erro;
        }
        public Error ValidateProductDescription(bool appendError = false)
        {
            Error erro = null;
            if (string.IsNullOrEmpty(this.ProductDescription) || this.ProductDescription.Length > 200)
            {
                erro = new Error { Description = "Descrição do produto incorrecta.", Field = "ProductDescription", TypeofError = this.GetType(), Value = this.ProductDescription, UID = this.Pk };
                if (appendError)
                    this.Tooltip.ProductDescription = this.Tooltip.ProductDescription.FormatTooltipWithError(erro.Description);
            }
            return erro;
        }
        public Error ValidateProductCode(bool appendError = false)
        {
            Error erro = null;
            if (string.IsNullOrEmpty(this.ProductCode) || this.ProductCode.Length > 60)
            {
                erro = new Error { Description = "Identificador do produto incorrecto.", Field = "ProductCode", TypeofError = this.GetType(), Value = this.ProductCode, UID = this.Pk };
                if (appendError)
                    this.Tooltip.ProductCode = this.Tooltip.ProductCode.FormatTooltipWithError(erro.Description);
            }
            return erro;
        }
        #endregion Validation
    }
}
